package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduCourseDescription;
import com.atguigu.eduservice.entity.frontvo.CourseFrontVo;
import com.atguigu.eduservice.entity.frontvo.CourseWebVo;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.PublishCourseVo;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseDescriptionService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author dujiajia
 * @since 2020-11-03
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private EduChapterService eduChapterService;
    // 添加课程信息和描述
    @Override
    public String addCourseInfo(CourseInfoVo courseInfoVo) {

        // 添加课程信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if(insert == 0){
            throw new GuliException(20001,"添加课程信息失败");
        }
        // 获取到上边添加的课程信息的id
        String cid = eduCourse.getId();
        // 添加课程描述信息
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(cid);
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescriptionService.save(eduCourseDescription);
        return cid;
    }

    // 根据id 获取课程信息
    @Override
    public CourseInfoVo getCourseById(String id) {
        System.out.println(id);
        // 创建 返回的vo实例
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        // 获取课程信息
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        EduCourse eduCourse = baseMapper.selectOne(wrapper);
        BeanUtils.copyProperties(eduCourse,courseInfoVo);

        // 获取课程描述
        EduCourseDescription eduCourseDescription = eduCourseDescriptionService.getById(id);
        courseInfoVo.setDescription(eduCourseDescription.getDescription());
        // 返回数据CourseInfoVo
        return courseInfoVo;
    }
    // 根据id修改课程信息
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        // 根据id更新课程信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int i = baseMapper.updateById(eduCourse);
        if (i == 0){
            throw new GuliException(20001,"更新课程信息失败");
        }
        // 根据id更新课程描述
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescriptionService.updateById(eduCourseDescription);

    }
    // 查询最终发布的课程全部信息
    @Override
    public PublishCourseVo getPublishCourse(String id) {
        PublishCourseVo publishCourseVo = baseMapper.selectPublishCourse(id);

        return publishCourseVo;
    }
    // 条件分页查询
    @Override
    public Page<EduCourse> queryCourse(Long pageNum, Long pageSize, String title) {
        // 创建page对象
        Page<EduCourse> pageCourse = new Page<>(pageNum,pageSize);
        // 创建查询对象
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        baseMapper.selectPage(pageCourse,wrapper);
        return pageCourse;
    }

    // 删除课程
    @Override
    public void deleteCourseById(String courseId) {
        // 1删除小节
        eduVideoService.removeByCourseId(courseId);

        // 2删除章节
        eduChapterService.removeChapterByCourseId(courseId);

        // 3删除课程描述
        eduCourseDescriptionService.removeById(courseId);

        // 4删除课程本身
        int i = baseMapper.deleteById(courseId);
        if (i == 0){
            throw new GuliException(20001,"删除课程失败");
        }

    }
    // 查询前端的 课程
    @Override
    @Cacheable(value = "courseIndex",key = "'selectCourseIndex'")
    public List<EduCourse> selectCourseIndex() {
        // 查询前8课程
        QueryWrapper<EduCourse> courseQueryWrapper = new QueryWrapper<>();
        courseQueryWrapper.orderByDesc("view_count");
        courseQueryWrapper.last("limit 8");
        List<EduCourse> courseList = baseMapper.selectList(courseQueryWrapper);
        return courseList;
    }

    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> coursePage, CourseFrontVo courseFrontVo) {

        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())){
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }
        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())){
            wrapper.orderByDesc("buy_count");
        }
        if(!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())){
            wrapper.orderByDesc("gmt_create");
        }
        if(!StringUtils.isEmpty(courseFrontVo.getPriceSort())){
            wrapper.orderByDesc("price");
        }

        baseMapper.selectPage(coursePage,wrapper);

        long current = coursePage.getCurrent();
        List<EduCourse> records = coursePage.getRecords();
        long size = coursePage.getSize();
        long total = coursePage.getTotal();
        long pages = coursePage.getPages();
        boolean hasNext = coursePage.hasNext();
        boolean hasPrevious = coursePage.hasPrevious();

        Map<String,Object> map = new HashMap<>();
        map.put("current",current);
        map.put("records",records);
        map.put("size",size);
        map.put("total",total);
        map.put("pages",pages);
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hasPrevious);

        return map;

    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.selectBaseCourseInfo(courseId);
    }


}
